package com.baitai.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baitai.common.utils.SecurityUtils;
import com.baitai.system.domain.ReceiveTask;
import com.baitai.system.service.IReceiveTaskService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baitai.common.annotation.Log;
import com.baitai.common.core.controller.BaseController;
import com.baitai.common.core.domain.AjaxResult;
import com.baitai.common.enums.BusinessType;
import com.baitai.system.domain.IssueTask;
import com.baitai.system.service.IIssueTaskService;
import com.baitai.common.utils.poi.ExcelUtil;
import com.baitai.common.core.page.TableDataInfo;

/**
 * 任务下发Controller
 * 
 * @author xx
 * @date 2024-07-14
 */
@RestController
@RequestMapping("/system/issueTask")
public class IssueTaskController extends BaseController
{
    @Autowired
    private IIssueTaskService issueTaskService;
    @Autowired
    private IReceiveTaskService receiveTaskService;


    /**
     * 查询任务下发列表
     */
    @PreAuthorize("@ss.hasPermi('system:issueTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(IssueTask issueTask)
    {
        startPage();
        List<IssueTask> list = issueTaskService.selectIssueTaskList(issueTask);
        return getDataTable(list);
    }

    /**
     * 导出任务下发列表
     */
    @PreAuthorize("@ss.hasPermi('system:issueTask:export')")
    @Log(title = "任务下发", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IssueTask issueTask)
    {
        List<IssueTask> list = issueTaskService.selectIssueTaskList(issueTask);
        ExcelUtil<IssueTask> util = new ExcelUtil<IssueTask>(IssueTask.class);
        util.exportExcel(response, list, "任务下发数据");
    }

    /**
     * 获取任务下发详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:issueTask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(issueTaskService.selectIssueTaskById(id));
    }

    /**
     * 新增任务下发
     */
    @PreAuthorize("@ss.hasPermi('system:issueTask:add')")
    @Log(title = "任务下发", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IssueTask issueTask)
    {
        return toAjax(issueTaskService.insertIssueTask(issueTask));
    }

    /**
     * 修改任务下发
     */
    @PreAuthorize("@ss.hasPermi('system:issueTask:edit')")
    @Log(title = "任务下发", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IssueTask issueTask)
    {
        return toAjax(issueTaskService.updateIssueTask(issueTask));
    }

    /**
     * 删除任务下发
     */
    @PreAuthorize("@ss.hasPermi('system:issueTask:remove')")
    @Log(title = "任务下发", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(issueTaskService.deleteIssueTaskByIds(ids));
    }


    /**
     * 查询任务下发列表
     */
    @GetMapping("/appList")
    public AjaxResult applist(IssueTask issueTask)
    {
        startPage();
        List<IssueTask> list = issueTaskService.selectIssueTaskList(SecurityUtils.getUserId(),issueTask);
        return AjaxResult.success(getDataTable(list));
    }

    /**
     * 接取任务
     */
    @Log(title = "接取任务", businessType = BusinessType.DELETE)
    @PostMapping("/receiveTask")
    public AjaxResult receiveTask(@RequestBody ReceiveTask relation){
        int result = issueTaskService.receiveTask(SecurityUtils.getUserId(),relation.getTaskId());
        if(result==1){
            return AjaxResult.error("任务接取人数已达上限");
        }else if(result ==2){
            return AjaxResult.error("任务已接取");
        }
        return AjaxResult.success("接取成功");
    }

    /**
     * 接取任务
     */
    @Log(title = "取消任务", businessType = BusinessType.DELETE)
    @PostMapping("/cancelReceiveTask")
    public AjaxResult cancelReceiveTask(@RequestBody ReceiveTask relation){
        int result = issueTaskService.cancelReceiveTask(SecurityUtils.getUserId(),relation.getTaskId());
        return AjaxResult.success("取消");
    }


    /**
     * 接取任务
     */
    @Log(title = "提交成功", businessType = BusinessType.DELETE)
    @PostMapping("/submitReceiveTask")
    public AjaxResult submitReceiveTask(@RequestBody ReceiveTask receiveTask){
        int result = receiveTaskService.submitReceiveTask(SecurityUtils.getUserId(),receiveTask);
        return AjaxResult.success("提交成功");
    }

}
